<?php
namespace Admin\Controller;
use Common\Model\ShopUserListModel;
use Common\Model\UserModel;
use Common\Util\BaseData;
use Common\Util\RefereeData;
use Common\Util\UserData;

class UserController extends AdminController{

	public function index(){
		$keyword = htmlspecialchars(trim($_GET['keyword']));
		$phone = htmlspecialchars(trim($_GET['phone']));
		$where = "1";
		//php获取昨日起始时间戳和结束时间戳
		$_GET['b_time'] = str_replace('%2B', ' ', $_GET['b_time']);
		$_GET['e_time'] = str_replace('%2B', ' ', $_GET['e_time']);
		if(!empty($_GET['b_time']) && empty($_GET['e_time'])){
			$time_b_yestoday = strtotime($_GET['b_time']);
			$time_e_yestoday = '';
		}elseif(empty($_GET['b_time']) && !empty($_GET['e_time'])){
			$time_b_yestoday = '';
			$time_e_yestoday = strtotime($_GET['e_time']);
		}elseif(!empty($_GET['b_time']) && !empty($_GET['e_time'])){
			$time_b_yestoday = strtotime($_GET['b_time']);
			$time_e_yestoday = strtotime($_GET['e_time']);
		}else{
			$time_b_yestoday = '';
			$time_e_yestoday = '';
		}
		$search_time_arr = [
			'b_time' => $time_b_yestoday ? date("Y-m-d H:i:s", $time_b_yestoday) : '',
			'e_time' => $time_e_yestoday ? date("Y-m-d H:i:s", $time_e_yestoday) : '',
		];
		if(!empty($time_b_yestoday) && empty($time_e_yestoday)){
			$where .= " and add_time>='$time_b_yestoday'";
		}elseif(empty($time_b_yestoday) && !empty($time_e_yestoday)){
			$where .= " and add_time<='$time_e_yestoday'";
		}elseif(!empty($time_b_yestoday) && !empty($time_e_yestoday)){
			$where .= " and add_time>='$time_b_yestoday' and add_time<='$time_e_yestoday'";
		}

		if(!empty($keyword)){
			$where .= " and (user_nickname like '%$keyword%')";
		}
		if(!empty($phone)){
			$where .= " and (user_true_phone = '{$phone}')";
		}
		//		判断用户类型
		if(!empty($_GET['user_level'])){
			if($_GET['user_level'] == -1){
				$where .= " and (user_level = 0)";
			}else{
				$where .= " and (user_level = {$_GET['user_level']})";
			}
		}
		$list = $this->lists('User', $where, 'id desc', true, 20);
		foreach($list as $key => $val){
			$list[$key]['user_headimg'] = $val['user_headimg'] ? $val['user_headimg'] : "http://xcx.tfxiaochengxu.com/2018-06-15_5b235fa500726.png";
			if($val['referrer_user_id'] > 0){
				$referee_info = UserData::getUserInfoById($val['referrer_user_id']);
				$list[$key]['referee_user_headimg'] = $referee_info['user_headimg'];
				$list[$key]['referee_user_nic
				kname'] = $referee_info['user_nickname'];
				$list[$key]['referee_user_true_phone'] = $referee_info['user_true_phone'];
			}
		}
		//        部分也查询会员收益
		$user_amount_list = M("user")->where($where)->select();
		$this->assign('list', $list);
		$this->assign("search_time_arr", $search_time_arr);
		$this->assign("keyword", $keyword);
		$this->assign("phone", $phone);
		$this->meta_title = '用户管理';
		$this->display();
	}

	public function userAccountLog(){
		$t_user_balance_log = M('user_balance_log');
		$this->meta_title = '资金流水';
		$map = [];
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$s_name = trim($_GET['name']);
			$map['u.user_nickname|u.user_true_phone'] = ['like', "%$s_name%"];
		}
		if(isset($_GET['user_id']) && !empty($_GET['user_id'])){
			$map['g.user_id'] = $_GET['user_id'];
		}

		//php获取昨日起始时间戳和结束时间戳
		$_GET['b_time'] = str_replace('%2B', ' ', $_GET['b_time']);
		$_GET['e_time'] = str_replace('%2B', ' ', $_GET['e_time']);
		if(!empty($_GET['b_time']) && empty($_GET['e_time'])){
			$time_b_yestoday = strtotime($_GET['b_time']);
			$time_e_yestoday = '';
		}elseif(empty($_GET['b_time']) && !empty($_GET['e_time'])){
			$time_b_yestoday = '';
			$time_e_yestoday = strtotime($_GET['e_time']);
		}elseif(!empty($_GET['b_time']) && !empty($_GET['e_time'])){
			$time_b_yestoday = strtotime($_GET['b_time']);
			$time_e_yestoday = strtotime($_GET['e_time']);
		}else{
			$time_b_yestoday = mktime(0, 0, 0, date('m'), date('d') - 360, date('Y'));
			$time_e_yestoday = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
		}

		$search_time_arr = [
			'b_time' => date("Y-m-d H:i:s", $time_b_yestoday), 'e_time' => date("Y-m-d H:i:s", $time_e_yestoday),
		];
		$this->assign('this_day_time', $search_time_arr);

		if(!empty($time_b_yestoday) && empty($time_e_yestoday)){
			$map['g.add_time'] = ['EGT', $time_b_yestoday];
		}elseif(empty($time_b_yestoday) && !empty($time_e_yestoday)){
			$map['g.add_time'] = ['ELT', $time_e_yestoday];
		}elseif(!empty($time_b_yestoday) && !empty($time_e_yestoday)){
			$map['g.add_time'] = ['BETWEEN', [$time_b_yestoday, $time_e_yestoday]];
		}

		$page = I('page', 1, 'intval');
		$row = 15;
		$data = $t_user_balance_log
			->alias('g')
			->join('left join '.C('DB_PREFIX').'user'.' u on u.id = g.user_id')
			->where($map)
			->field("g.*,u.user_nickname,u.user_headimg,u.user_true_phone")
			->order("g.id desc")
			->page($page, $row)
			->select();
		/* 查询记录总数 */
		$count = $t_user_balance_log
			->alias('g')
			->join('left join '.C('DB_PREFIX').'user'.' u on u.id = g.user_id')
			->where($map)
			->count();
		$list_data ['list_data'] = $data;
		// 分页
		if($count > $row){
			$page = new \Think\Page ($count, $row);
			$page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$list_data ['_page'] = $page->show();
		}
		$this->assign('list_data', $list_data);
		$this->assign('getting', $_GET);
		$this->display('user_account_log');
	}

	public function userInfo(){
		$user_m = new UserModel();
		if(IS_AJAX){
			$user_id = $_POST['user_id'] ? intval($_POST['user_id']) : 0;
			$user_true_name = $_POST['user_true_name'] ? trim($_POST['user_true_name']) : '';
			$user_true_phone = $_POST['user_true_phone'] ? trim($_POST['user_true_phone']) : '';
			if(!empty($user_true_phone)){
				if(isMobile($user_true_phone) == false) $this->return_json_data(0, '手机号码输入错误');
				//验证手机号码是否存在
				$is_phone_map['user_true_phone'] = $user_true_phone;
				$is_phone_map['id'] = ['neq', $user_id];
				if($isPhone = M('user')->where($is_phone_map)->find()){
					$this->return_json_data(0, '手机号码已存在，请更换');
				}
			}
			$s_u_data['user_true_name'] = $user_true_name;
			$s_u_data['user_true_phone'] = $user_true_phone;
			$s_u_data['user_login_status'] = $_POST['user_login_status'] ? intval($_POST['user_id']) : 1;
			$s_u_data['update_time'] = time();
			if(M('user')->where("id=$user_id")->save($s_u_data)){
				$this->return_json_data(1, '更新成功');
			}
			$this->return_json_data(0, '更新失败，请刷新重试');
		}
		$user_id = $_GET['id'] ? intval($_GET['id']) : 0;
		$user_info = $user_m->detail($user_id);
		$user_info['add_time'] = time_format($user_info['add_time']);
		$this->assign('user_info', $user_info);
		$this->display('user_info');
	}

	//取消身份
	public function cacelUserType(){
		$user_id = $_GET['user_id'] ? intval($_GET['user_id']) : 0;
		$s_u_data['user_type'] = 0;
		$s_u_data['update_time'] = time();
		if(M("user")->where("id=$user_id")->save($s_u_data)){
			$this->return_json_data(1, '取消成功');
		}else{
			$this->return_json_data(0, '取消失败，请刷新重试');
		}
	}
}

?>
